package ext.generic.wfaugment;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;
import wt.org.WTOrganization;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.project.Role;
import wt.session.SessionServerHelper;
import wt.team.Team;
import wt.team.TeamManaged;
import wt.team.TeamReference;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.forms.FormResultAction;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;

public class WorkFlowSetParticipantsProcessor extends DefaultObjectFormProcessor {
	private static String RESOURCE = "ext.generic.wfaugment.workflowStepRB";
	public static String CLASSNAME="";
	public static boolean VERBOSE = false;
	public static WTProperties wtProperties;

	static
	{

		try
		{
			CLASSNAME= WorkFlowSetParticipantsProcessor.class.getName();
			wtProperties = WTProperties.getLocalProperties();
			VERBOSE = wtProperties.getProperty("ext.generic.workflowSteps.verbose", false);
		}
		catch (java.io.IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
	public WorkFlowSetParticipantsProcessor()
	{

	}
	  /**
	 *输出调试信息
	 */
	private static void outDebugInfo(String info)
	{
		if(VERBOSE)
			System.out.println(CLASSNAME+"......"+info);
	}	
	
	public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException
	{
		WTOrganization wtOrg;
		boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
		FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
		formresult.setNextAction(FormResultAction.REFRESH_OPENER);
		try
		{
			NmOid nmoid = nmcommandbean.getActionOid();
			if (nmoid == null)
				nmoid = nmcommandbean.getPrimaryOid();
			WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
			outDebugInfo("  contextObj="+contextObj);
			if (contextObj instanceof WorkItem)
			{
				//Team team = (Team) contextObj;
				WorkItem workItem=(WorkItem)contextObj;
				//Persistable persistable = workItem.getPrimaryBusinessObject().getObject();
				WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
				WfProcess wfprocess = wfactivity.getParentProcess();
				Team team = (Team) ((TeamReference) ((TeamManaged) wfprocess).getTeamId()).getObject();	
				String[] roletype = (String[]) nmcommandbean.getParameterMap().get("roletype");
				Role role = Role.toRole(roletype[0]);
				outDebugInfo("  ReOperat to team="+team+" role="+role);
				Enumeration enums=team.getPrincipalTarget(role);
				while(enums != null && enums.hasMoreElements())
				{
					WTPrincipal wtp = ((WTPrincipalReference)enums.nextElement()).getPrincipal();
					team.deletePrincipalTarget(role,wtp);
					outDebugInfo("  delete WTPrincipal="+wtp.getDisplayIdentifier()+" from role="+role);
				}
				team = (Team)PersistenceHelper.manager.refresh(team);
				ArrayList arraylist = nmcommandbean.getSelected();
				outDebugInfo(" getSelected arraylist=" + arraylist);
				for (int i = 0; i < arraylist.size(); i++)
				{
					NmContext nmcontext = (NmContext) arraylist.get(i);
					Persistable obj = nmcontext.getTargetOid().getWtRef().getObject();
					if (obj instanceof WTPrincipal)
					{
						WTPrincipal wtpforadd=(WTPrincipal)obj;
						team.addPrincipal(role, wtpforadd);
						outDebugInfo("  add WTPrincipal="+wtpforadd.getDisplayIdentifier()+" to role="+role);
					}
				}
			}
			return formresult;
		}
		finally
		{
			SessionServerHelper.manager.setAccessEnforced(flag);
		}
	}
}
